package com.wzy.controller;

import java.util.Arrays;

/**
 * @Author: wzy
 * @Date: 2022/01/05/20:20
 * @Description:
 */
public class TestLee {
    public static void main(String[] args) {
        int[][] nums = {{}};
        int[] ints = spiralOrder(nums);
        System.out.println(Arrays.toString(ints));

    }

    public static int[] spiralOrder(int[][] matrix) {
        if (matrix == null || matrix.length ==0 || matrix[0].length == 0) {
            return new int[0];
        }
        int currL = 0, left = 0, right = matrix[0].length - 1;
        int currU = 0, up = 0, down = matrix.length - 1;
        int count = 0;
        int length = (down + 1) * (right + 1);
        int[] nums = new int[length];
        while (left <= right && up <= down) {
            // 右
            if (currL <= right) {
                while (currL <= right) {
                    nums[count++] = matrix[currU][currL];
                    currL++;
                }
                currL--;
                currU++;
                up++;
            }
            if (count == length) {
                break;
            }
            // 下
            if (currU <= down) {
                while (currU <= down) {
                    nums[count++] = matrix[currU][currL];
                    currU++;
                }
                currU--;
                currL--;
                right--;
            }

            if (count == length) {
                break;
            }
            // 左
            if (currL >= left) {
                while (currL >= left) {
                    nums[count++] = matrix[currU][currL];
                    currL--;
                }
                currL++;
                currU--;
                down--;
            }
            if (count == length) {
                break;
            }
            // 上
            if (currU >= up) {
                while (currU >= up) {
                    nums[count++] = matrix[currU][currL];
                    currU--;
                }
                left++;
                currU++;
                currL++;
            }
            if (count == length) {
                break;
            }
        }
        return nums;
    }
}
